package com.sec.android.app.myfiles.widget.tablet;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.util.PreferenceUtils;

/**
 * Created by jaebae on 2016-01-14.
 */
public class ColumnHeaderItemView extends RelativeLayout {
    private TextView mColumnNameView;
    private ImageView mSortView;
    private int mSortType;

    private int mMinWidth;

    public ColumnHeaderItemView(Context context) {
        super(context);
        init();
    }

    public ColumnHeaderItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ColumnHeaderItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public ColumnHeaderItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.column_list_header_item, this);
        mColumnNameView = (TextView) findViewById(R.id.column_name);
        float destMaxFontSize = getResources().getDimension(R.dimen.list_column_header_text_max_size);
        if (mColumnNameView.getTextSize() > destMaxFontSize) {
            mColumnNameView.setTextSize(TypedValue.COMPLEX_UNIT_PX, destMaxFontSize);
        }
        mSortView = (ImageView) findViewById(R.id.sort_arrow);
    }

    public void setText(String text) {
        mColumnNameView.setText(text);

        Paint paint = mColumnNameView.getPaint();
        float width = paint.measureText("A...");
        mMinWidth = (int) width;
        mMinWidth += getContext().getResources().getDimensionPixelSize(R.dimen.list_column_header_text_margin_start);
        mMinWidth += getContext().getResources().getDimensionPixelSize(R.dimen.list_column_header_arrow_width);
        mMinWidth += getContext().getResources().getDimensionPixelSize(R.dimen.list_column_header_arrow_margin);
    }

    public void setSortType(int sortType) {
        mSortType = sortType;
    }

    public int getSortType() {
        return mSortType;
    }

    public int setWidth(int nWidth) {
        if (nWidth < mMinWidth) {
            nWidth = mMinWidth;
        }
        ViewGroup.LayoutParams params = getLayoutParams();
        params.width = nWidth;
        setLayoutParams(params);
        return nWidth;
    }

    public void refreshSortCursor(boolean sortEnabled, NavigationInfo naviInfo) {
        Context context = getContext();
        int sortType = PreferenceUtils.getSortByType(context, naviInfo);
        if (sortType == mSortType && sortEnabled) {
            mSortView.setVisibility(View.VISIBLE);

            int sortOrder = PreferenceUtils.getSortByOrder(context, naviInfo);

            int resId = R.drawable.myfiles_detail_info_btn_open;
            if (sortOrder == AppConstants.SortByOrder.DESC) {
                resId = R.drawable.myfiles_detail_info_btn_close;
            }
            mSortView.setImageResource(resId);
            mColumnNameView.setTextColor(getResources().getColor(R.color.list_column_header_text_color_enabled));
            mColumnNameView.setTypeface(mColumnNameView.getTypeface(), Typeface.BOLD);
        } else {
            mSortView.setVisibility(View.GONE);
            mColumnNameView.setTextColor(getResources().getColor(R.color.list_column_header_text_color_disabled));
            mColumnNameView.setTypeface(mColumnNameView.getTypeface(), Typeface.NORMAL);
        }
    }
}
